package com.wk.warehouse.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import com.wk.warehouse.entity.InStore;
import com.wk.warehouse.entity.Purchase;
import com.wk.warehouse.entity.Result;
import com.wk.warehouse.entity.Store;
import com.wk.warehouse.service.InStoreService;
import com.wk.warehouse.service.StoreService;
import com.wk.warehouse.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * 入库管理后台
 */
@RestController
@RequestMapping("/instore")
public class InstoreController {

	@Autowired
	private InStoreService inStoreService;
	@Autowired
	private StoreService storeService;

	/**
	 * 所有仓库
	 */
	@GetMapping("/store-list")
	public Result storeList() {
		return Result.ok(storeService.storeList());
	}

	/**
	 * 分页模糊查询所有入库单
	 */
	@GetMapping("/instore-page-list")
	public Result inStorePageList(InStore inStore, Page page) {
		// 获取总条数
		int totalNum = inStoreService.selectCount(inStore);
		page.setTotalNum(totalNum);

		// 查询入库单
		List<InStore> inStoreList = inStoreService.selectInStorePage(inStore, page);
		page.setResultList(inStoreList);
		return Result.ok(page);
	}

	/**
	 * 导出数据
	 */
	@GetMapping("/exportTable")
	public Result exportTable(InStore inStore, Page page){
		page = new Page(9999, 1); // 导出模糊查询所有数据
		// 要导出的数据
		List<InStore> inStoreList = inStoreService.selectInStorePage(inStore, page);
		return Result.ok(inStoreList);
	}

	/**
	 * 确定入库
	 */
	@PutMapping("/instore-confirm")
	public Result confirmInsStore(@RequestBody InStore inStore) {
		// 修改库存数量
		inStoreService.updateStoreInvent(inStore);
		// 修改入库状态
		inStoreService.updateInStoreState(inStore.getInsId());
		return Result.ok("入库成功！");
	}
}

